package com.fjwt.gz.mch.ctrl.proxy;

import cn.hutool.json.JSONUtil;
import com.alibaba.fastjson2.JSONObject;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.fjwt.gz.core.constants.ApiCodeEnum;
import com.fjwt.gz.core.constants.Constants;
import com.fjwt.gz.core.model.ApiRes;
import com.fjwt.gz.core.model.security.GzUserDetails;
import com.fjwt.gz.db.entity.ActivityEntity;
import com.fjwt.gz.db.vo.ActivityAndCouponVO;
import com.fjwt.gz.db.vo.ActivityVO;
import com.fjwt.gz.mch.ctrl.CommonCtrl;
import com.fjwt.gz.service.impl.ActivityService;
import com.fjwt.gz.service.impl.MchInfoService;
import com.fjwt.gz.zhyx.SignUtils;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Objects;

@Slf4j
@RestController
@RequestMapping("/api/proxy/activity")
public class ActivityProxyController extends CommonCtrl {

    @Autowired
    private ActivityService activityService;

    @Autowired
    private MchInfoService mchInfoService;


    /**
     * 机构端增加扫码派券活动列表
     *
     * @return
     */
    @PostMapping("/sendToMchUserActivity")
    public ApiRes sendToMchUserActivity() {
        String templateNo = getValString("templateNo");
        String activityNo = getValString("activityNo");
        String activityName = getValString("activityName");
        String startTime = getValString("startTime");
        String endTime = getValString("endTime");
        Long fansId = getCurrentUser().getFansId();
        String appId = request.getHeader("appid");
        log.info("机构端增加扫码派券活动列表接口调用，请求地址：/api/proxy/activity/sendToMchUserActivity，请求参数：模板编号：{}，活动编号：{}，活动名称：{}，开始时间：{}，结束时间：{}",
                templateNo, activityNo, activityName, startTime, endTime);
        GzUserDetails currentUser = getCurrentUser();
        List<ActivityAndCouponVO> list = activityService.sendToMchUserActivityListV2(templateNo, activityNo,
                activityName, startTime, endTime, currentUser.getUserId(), fansId, appId);
        log.info("机构端增加扫码派券活动列表接口返回，请求地址：/api/proxy/activity/sendToMchUserActivity，返回结果：{}", JSONUtil.toJsonStr(list));
        List<ActivityAndCouponVO> vos = List.copyOf(list);
        return ApiRes.ok(vos);
    }


    /**
     * 获取当前活动，已结束活动
     *
     * @return
     */
    @PostMapping("/getActivityList")
    public ApiRes getActivityList() {
        String flag = getValString("flag");
        log.info("机构获取当前0/1已结束活动，请求地址：/api/proxy/activity/getActivityList，活动状态：{}", flag);
        if (StringUtils.isEmpty(flag)) {
            return ApiRes.customFail("网络故障，请稍后重试！");
        }
        Long fansId = getCurrentUser().getFansId();
        String appId = getCurrentUser().getAppId();
        IPage<ActivityVO> list = activityService.getActivityList(getIPage(), flag, fansId, appId);
        return ApiRes.page(list);
    }

    /**
     * 根据商户号查询活动
     *
     * @return
     */
    @PostMapping("/getActivityMchNo")
    public ApiRes getActivityMchNo() {
        String mchNo = getValString("mchNo");
        log.info("商户参与活动记录接口调用，请求路径：/api/proxy/activity/getActivityMchNo，商户号：{}", mchNo);
        if (StringUtils.isEmpty(mchNo)) {
            return ApiRes.customFail("网络故障，请稍后重试！");
        }
        IPage<ActivityEntity> list = activityService.fansGetActivityByMchNo(getIPage(), mchNo, 4);
        return ApiRes.page(list);
    }


    /**
     * 获取活动详情信息
     */
    @PostMapping("/getActivityDetail")
    public ApiRes getActivityDetail(){
        Long activityId = getValLong("activityId");
        log.info("获取活动详情接口，请求地址：/api/proxy/activity/getActivityDetail，活动id：{}",activityId);
        if(Objects.isNull(activityId)){
            return ApiRes.customFail("网络故障，请稍后重试！");
        }
        ActivityVO activityEntity = activityService.getActivityDetail(activityId);
        ActivityVO dto = new ActivityVO();
        BeanUtils.copyProperties(activityEntity,dto);
        return ApiRes.ok(dto);
    }

}
